var table ,form ,laypage ,layer ,element ,laydate ,upload ,colorpicker//时间组件
$(function(){
	layui.use(['table','form','laypage','layer', 'element', 'laydate','upload','colorpicker'], function(){
		  table = layui.table;
          form=layui.form;
          laypage = layui.laypage ;//分页
          layer = layui.layer ;//弹层
          element = layui.element; //元素操作
          laydate = layui.laydate; //时间组件 
          upload = layui.upload; 
		  colorpicker = layui.colorpicker;
		//上级菜单选择
		var setting = {
			view: {
				dblClickExpand: false
			},
			data: {
				simpleData: {
					enable: true,
					idKey : "id",
					pIdKey: "pid"
				},
				key: {
					url:'none'
				}
			},
			callback: {
				onClick: onClick,
				onAsyncSuccess: function(event, treeId, treeNode, msg) {
				    $.fn.zTree.getZTreeObj(treeId).expandAll(true);//异步加载完成后展开tree
				}
			}
		};
		$.ajax({
	        url: "/equipmentType/getList.do",               //请求地址
	        type: "POST",              //请求类型
	        data:{},              //请求数据
	        timeout: 200000,                //请求超时时间(毫秒)
	        headers: setHeader(),
	        success:function(res){         //请求成功
	     	   if(res.status == 200){
	     		  var zNodes=res.resultData.data
	     		  var zTree = $.fn.zTree.init($("#treeDemo"), setting ,zNodes);
	     		 zTree.expandAll(true);//节点展开
			   }else if(res.status == 666){
				   TIMEOUT(); 
		       }else{
  					layer.msg(res.resultMsg, {icon: res.icon});
  				} 
	       }
	    })
		//上级菜单选择end
	  //表单验证
	  form.verify({
//		  fpid: function(value, item){ //上级菜单验证
//				if(validate.isBlank(value))
//				{
//					return "请选择父级类型！";
//				}
//		  },
		  fname: function(value, item){ //名称验证
				if(validate.isBlank(value))
				{
					return "请输入类型名称！";
				}
				if (!validate.isLength(value, 1, 50)) {
			      return '类型名称长度不能超过50个字符';
			    }
		  }
	  	  ,fremarks: function(value, item)//描述验证
		  {
	  		   if (!validate.isLength(value, 0, 200)) {
			    {
			      return '备注长度不能超过200个字符';
			    }
		  }
		}
	  });
	  
	  //表单提交事件
	  form.on('submit(formDemo)', function(data){
		    this.blur();//防止点回车，按钮重复点击
		  	var loadIndex = layer.load(1);//显示加载层
		  	var requestData = formToJsonObj("form_edit", []);
		  	$.ajax({
		           url: "/equipmentType/saveEquipmentType.do",               //请求地址
		           type: "POST",              //请求类型
		           data:formatParam(requestData),              //请求数据
		           timeout: 200000,                //请求超时时间(毫秒)
		           headers: setHeader(),
		           success:function(res){         //请求成功
		        	   if(res.status == 200)
		  				{
		    				layer.msg(res.resultMsg, {icon: res.icon},function(){
								     var index = parent.layer.getFrameIndex(window.name);
								     window.parent.renderTable();//执行父页面的事件
								      parent.layer.close(index);//关闭弹出的子页面窗口
							});
		  				}else if(res.status == 666){
		  				  TIMEOUT(); 
		  		       }else{
		  					layer.msg(res.resultMsg, {icon: res.icon});
		  				} 
		        	   layer.close(loadIndex);//关闭加载层      
	              }
	           })
			return false; //阻止表单跳转。
		});
	});
})
 function onClick(e, treeId, treeNode) {
			$("#pname").val(treeNode.name);
			$("#pid").val(treeNode.id);
			hideMenu();
}
function showMenu() {
	var cityObj = $("#pname");
	var cityOffset = $("#pname").offset();
	$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
	$('ul.ztree').width(cityObj.width());//设置下拉菜单宽度	
	$("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}


